﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using SYcwpb.Commands;
using System.Windows;
using System.Threading;
using System.Data;
using System.Collections;
using SYcwpb.Views;

namespace SYcwpb.ViewModels
{
    public class MonthlyPlanModel : ViewModelBase
    {
        public Thread ShowTableThread = null;
        private MonthlyPlan _viewMonthlyPlan = new MonthlyPlan();
        private DriverTeamViewModel _driverTeamViewModel = null;

        private ICommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (null == _saveCommand)
                    _saveCommand = new RelayCommands(para => this.SaveCommand_Method(), para => CanExecute());

                return _saveCommand;
            }
        }

        public void SaveCommand_Method()
        {
            //在这里写
        }

        //该方法不用
        private ICommand _showCommand;
        public ICommand ShowCommand
        {
            get
            {
                if (null == _showCommand)
                    _showCommand = new RelayCommands(para => this.ShowCommand_Method(), para => CanExecute());

                return _showCommand;
            }
        }

        public void ShowCommand_Method()
        {
            if (null == ShowTableThread)
            {
                ShowTableThread = new Thread(new ThreadStart(ShowTable));
                ShowTableThread.SetApartmentState(ApartmentState.STA);
                ShowTableThread.IsBackground = true;
            }
            else
                ShowTableThread.Abort();

            ShowTableThread.Start();

            IsExporting = true;
            ExportInfoVisibility = Visibility.Visible;

        }

        private void ShowTable()
        {
           /* if (string.IsNullOrEmpty(_viewMonthlyPlan.comboBox1.Text))
            {
                MessageBox.Show("请选择计划班表");
            }
            if (string.IsNullOrEmpty(_viewMonthlyPlan.startDay.Text) || string.IsNullOrEmpty(_viewMonthlyPlan.endDay.Text))
            {
                MessageBox.Show("请输入起止日期");
            }
            else
            {
                IsExporting = true;
                ExportInfoVisibility = Visibility.Visible;
                string[] date = _viewMonthlyPlan.comboBox1.Text.Split('-');
                int year = Convert.ToInt32(date[0].ToString());
                int month = Convert.ToInt32(date[1].ToString());
                int totalday = DateTime.DaysInMonth(year, month);
                int startday = Convert.ToInt32(_viewMonthlyPlan.startDay.Text.ToString());
                int endday = Convert.ToInt32(_viewMonthlyPlan.endDay.Text.ToString());

                if (startday > totalday || startday < 1 || endday > totalday || endday < 0)
                {
                    MessageBox.Show("请输入合法的日期数，如：1，2，3……");
                    return;
                }
                if (startday > endday)
                {
                    MessageBox.Show("开始日期不能大于结束日期");
                    return;
                }

                //获取数据表
                DataTable dt = _driverTeamViewModel.GetDriverCombineCrossingRoadPlane(year, month, startday, endday);
                //获取时刻表的名称
                Hashtable hasTbleNameWithDate = _driverTeamViewModel.GetTableNameWithDate(year, month, startday, endday);
                //绑定数据
                _viewMonthlyPlan.DataBind(dt, hasTbleNameWithDate, _viewMonthlyPlan.dataGrid);
                _viewMonthlyPlan.dataGrid.FrozenColumnCount = 3;
            }

            IsExporting = false;
            ExportInfoVisibility = Visibility.Collapsed;*/
        }


        public bool CanExecute()
        {
            return true;
        }

        private bool _isExporting = false;
        public bool IsExporting
        {
            get { return !_isExporting; }
            set
            {
                if (value == _isExporting)
                    return;

                _isExporting = value;

                OnPropertyChanged("IsExporting");
            }
        }


        private Visibility _exportInfoVisibility = Visibility.Collapsed;
        public Visibility ExportInfoVisibility
        {
            get { return _exportInfoVisibility; }
            set
            {
                if (value == _exportInfoVisibility)
                    return;

                _exportInfoVisibility = value;

                OnPropertyChanged("ExportInfoVisibility");
            }
        }
    }
}
